﻿@using Common
@model Param_AdminUser


<link href="https://cdn.staticfile.org/layui/2.7.6/css/layui.css" rel="stylesheet">
<link rel="stylesheet" href="/Content/layuiadmin/style/admin.css" media="all">

<div class="layui-fluid">
    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-col-xs10 layui-col-xs-offset1">
                <form class="layui-form" lay-filter="form" style="margin-right:60px;" onsubmit="return false">
                    <div class="layui-form-item">
                        <label for="AdminName" class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="text" name="adminname" id="AdminName" required lay-verify="required" placeholder="请输入名称" value="@Model.AdminName" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="PassWord" class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="password" id="PassWord" required lay-verify="required" placeholder="请输入密码" value="@Model.PassWord" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="HeadImg" class="layui-form-label">头像</label>
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" id="test1">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button> <br />
                            <input type="image" onclick="openimg('@(Tools.HeadImg+Model.HeadImg)')" src="@(Tools.HeadImg+Model.HeadImg)" id="HeadImg" style="max-height:100px;" lay-verify="required" value="@Model.HeadImg" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="NickName" class="layui-form-label">昵称</label>
                        <div class="layui-input-block">
                            <input type="text" name="nickname" id="NickName" required lay-verify="required" placeholder="请输入昵称" value="@Model.NickName" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="IsEnable" class="layui-form-label">启用</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="IsEnable" lay-skin="switch" @(Model.IsEnable.Value ? "checked" : "") lay-text="开启|关闭">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="Power" class="layui-form-label">身份</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="Power" lay-skin="switch" @(Model.Power.Value ? "checked" : "") lay-text="超管|员工">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn sub" lay-submit lay-filter="formDemo">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.staticfile.org/layui/2.7.6/layui.js"></script>

<script>
    layui.config({
        base: '/Content/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'set']);
</script>

<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/dist/html5ImgCompress.min.js"></script>
<script src="~/Scripts/tools.js"></script>
<script>
    var data = {};
    //Demo
    $(function () {
        var form = layui.form;
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , url: '' //上传接口
            , auto: false//是否自动上传
            , bindAction: "#BTN"
            , size: 2048 //上传图片大小限制 KB
            , number: 1 //同时上传数量
            , accept: "images" //文件类型
            , acceptMime: 'image/*'
            , choose: function (obj) {
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                obj.preview(function (index, file, result) {
                    //console.log(index); //得到文件索引
                    console.log(file); //得到文件对象
                    //console.log(result); //得到文件base64编码，比如图片

                    new html5ImgCompress(file, {
                        before: function (file) {
                            console.log('压缩前...');
                            // 这里一般是对file进行filter，例如用file.type.indexOf('image') > -1来检验是否是图片
                            // 如果为非图片，则return false放弃压缩（不执行后续done、fail、complete），并相应提示
                            if (file.type.indexOf('image') == -1) {
                                layer.msg("图片格式错误", { icon: 5, anim: 6 });
                                return false;
                            }
                        },
                        done: function (file, base64) {
                            console.log('压缩成功...');
                            //console.log(base64);
                            data.HeadImg = base64;
                            $("#HeadImg").attr("src", base64).val(base64);
                            $("#HeadImg").removeAttr("onclick");
                            $("#HeadImg").unbind();
                            $("#HeadImg").click(function () {
                                openimg(`${base64}`);
                                return false;
                            })
                            // ajax和服务器通信上传base64图片等操作
                        },
                        fail: function (file) {
                            console.log('压缩失败...');
                        },
                        complete: function (file) {
                            console.log('压缩完成...');
                        },
                        notSupport: function (file) {
                            console.log('浏览器不支持！')
                            // 不支持操作，例如PC在这里可以采用swfupload上传
                        }
                    });

                    //obj.resetFile(index, file, '123.jpg'); //重命名文件名，layui 2.3.0 开始新增

                    //这里还可以做一些 append 文件列表 DOM 的操作

                    //obj.upload(index, file); //对上传失败的单个文件重新上传，一般在某个事件中使用
                    //delete files[index]; //删除列表中对应的文件，一般在某个事件中使用
                });

            }
            , done: function (res) {
                //上传完毕回调
            }
            , error: function () {
                //请求异常回调
            }
        });

        form.on('submit(form)', function () {
            // 验证均通过后的操作（如 Ajax 提交）
            // …
            data.HeadImg = $("#HeadImg").val();
            if (data.HeadImg == undefined || data.HeadImg == "") {
                console.log(data.HeadImg);
                layer.msg("图片不能为空", { icon: 5, anim: 6 });
                return false;
            }

            var index = layer.load(1, { shade: [0.6, "#fff"] });
            data.AdminName = $("#AdminName").val();
            data.PassWord = $("#PassWord").val();
            data.NickName = $("#NickName").val();
            data.IsEnable = $("#IsEnable").prop("checked");
            data.Power = $("#Power").prop("checked");
            console.log(data);

            $.post(apiurl + "api/AdminUser/Update/" + @Model.AdminID, data, function (res) {
                console.log(res);
                if (res.Code == 200) {
                    setTimeout(() => {
                        layer.close(index);
                        layer.msg("编辑成功", { icon: 6, time: 1500 });
                        setTimeout(() => {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.location.reload();
                        }, 600)
                    }, 300);
                }
                else {
                    layer.msg("编辑失败");
                }
            })
        });
    })
</script>
